//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    imgUrls: [
      '../../images/banner.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    menulist: ['女性健康', '老年人健康', '儿童健康', '个性化用药', '遗传/生殖', '肿瘤', '心血管', '内分泌','消化'],
    indicatorDots: true,
    autoplay: true,
    isdisplay:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    interval: 5000,
    duration: 1000,
    trUserInfo:{},
    searchValue: '',
    hasUserInfo: false
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var trUserInfo = wx.getStorageSync('trUserInfo');
    if (app.globalData.userInfo) {
      this.setData({
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            hasUserInfo: true
          })
        }
      })
    }
  },
  onShow: function () {
    var that = this;
    wx.request({
      url: getApp().globalData.host_url + '/healthCheck/ajax_healthCheckList',
      method: 'GET',
      data: {},
      success: function (res) {
        var list = res.data.data;
        if (list == null) {
          var textToast = "信息获取失败！";
          wx.showToast({
            title: textToast,
            icon: '',
            duration: 2000
          })
        } else {
          that.setData({
            menulist: list,
          })
        }
      },
      fail: function (res) {
        that.setData({
          isdisplay: 1,
        })
      }
    })
  },
  searchValueInput: function (e) {
    var value = e.detail.value;
    this.setData({
      searchValue: value,
    });
  },
  search_view: function (e) {
    var input_val = this.data.searchValue;
    var flag = true;
    for (var i = 0; i < this.data.menulist.length; i++){
      if (this.data.menulist[i].checkName.indexOf(input_val) != -1){
        var k = i + 1; 
        wx.navigateTo({
          url: '../checkItem0' + k + '/checkItem0' + k
        })
        flag = false;
        break;
      }
    }
    if(flag){
      wx.showToast({
        title: '暂无该栏目内容',
        icon: 'loading',
        duration: 2000
      })
    }
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
